Include a header file required for build on mac 10.4
[supercollider.git] / Help / Tutorials / Getting-Started / Functions and Other Functionality.html
blobf68287c42375a34fc698833f4f9f33043dbdccd4
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta http-equiv="Content-Style-Type" content="text/css">
6 <title></title>
7 <meta name="Generator" content="Cocoa HTML Writer">
8 <meta name="CocoaVersion" content="824.42">
9 <style type="text/css">
10 p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica}
11 p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}
12 p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
13 p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco}
14 p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #606060}
15 p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; min-height: 12.0px}
16 p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #a71e12}
17 p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #a71e12; min-height: 12.0px}
18 p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #0019b7; min-height: 12.0px}
19 p.p10 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; color: #0000ff}
20 span.s1 {font: 12.0px Helvetica}
21 span.s2 {color: #0019b7}
22 span.s3 {font: 12.0px Helvetica; color: #000000}
23 span.s4 {color: #000000}
24 span.s5 {font: 9.0px Monaco}
25 span.s6 {color: #606060}
26 span.s7 {font: 9.0px Monaco; color: #a71e12}
27 span.s8 {font: 9.0px Monaco; color: #0019b7}
28 span.s9 {color: #a71e12}
29 span.s10 {color: #0000ff}
30 span.Apple-tab-span {white-space:pre}
31 </style>
32 </head>
33 <body>
34 <p class="p1"><b>Functions and Other Functionality</b></p>
35 <p class="p2"><br></p>
36 <p class="p3">The easiest way to get sound from SC is to use a Function. Below is a simple example of this. Execute this (after making sure the server is booted), and when you're sick of it, press Cmd - . (that's hold down the command key and press the period or fullstop key) to stop the sound. This will always stop all sound in SC. You'll be using it a lot, so commit it to memory.</p>
37 <p class="p2"><br></p>
38 <p class="p4"><span class="s1"><span class="Apple-tab-span"> </span></span>{ [<span class="s2">SinOsc</span>.ar(440, 0, 0.2), <span class="s2">SinOsc</span>.ar(442, 0, 0.2)] }.play;</p>
39 <p class="p2"><br></p>
40 <p class="p3">Not too inspiring? Don't worry, we're just getting started, and this is just a simple example to demonstrate Functions and sound. We'll take it apart a bit below.<span class="Apple-converted-space"> </span></p>
41 <p class="p2"><br></p>
42 <p class="p3">Before we get to doing that though, let's learn a little about Functions in general.<span class="Apple-converted-space"> </span></p>
43 <p class="p2"><br></p>
44 <p class="p3">A Function is just a reusable bit of code. You define a Function by enclosing code in 'curly brackets': { }. Here's an example:</p>
45 <p class="p2"><br></p>
46 <p class="p5"><span class="s3"><span class="Apple-tab-span"> </span></span><span class="s4">f = { </span>"Function evaluated"<span class="s4">.postln; };</span></p>
47 <p class="p2"><br></p>
48 <p class="p3">The stuff within the curly brackets is what will get executed each time you reuse, or evaluate the Function. Note that this is written like an equation, i.e. <span class="s5">f = {...}</span>. This is not an equation in the mathematical sense, it's what's called an assignment. Basically it allows me to name the Function I've created, by storing it in a variable called '<span class="s5">f</span>'. A variable is just a name representing a slot in which we can store things, such as a Function, a number, a list, etc. Execute the following lines one at a time and watch the post window:</p>
49 <p class="p2"><br></p>
50 <p class="p5"><span class="s3"><span class="Apple-tab-span"> </span></span><span class="s4">f = { </span>"Function evaluated"<span class="s4">.postln; };</span></p>
51 <p class="p4"><span class="Apple-tab-span"> </span>f;</p>
52 <p class="p2"><br></p>
53 <p class="p3">Both times it should say '<span class="s5">a Function</span>'. Now whenever we want to refer to our Function we can just use the letter f. That's in fact what makes it reusable! Otherwise we'd need to type the Function in every time.</p>
54 <p class="p2"><br></p>
55 <p class="p3">So how do we reuse it? Execute the following lines one at a time and watch the post window:</p>
56 <p class="p2"><br></p>
57 <p class="p5"><span class="s4"><span class="Apple-tab-span"> </span>f = { </span>"Function evaluated"<span class="s4">.postln; };</span></p>
58 <p class="p4"><span class="Apple-tab-span"> </span>f.value;</p>
59 <p class="p4"><span class="Apple-tab-span"> </span>f.value;</p>
60 <p class="p4"><span class="Apple-tab-span"> </span>f.value;</p>
61 <p class="p2"><br></p>
62 <p class="p3">Our Function is an object, (i.e a thing that does something or represents something), which we have defined and stored in the variable '<span class="s5">f</span>'. The bit of code that says '<span class="s5">.value</span>' says evaluate this function now. This is an example of sending a message to an object. This follows the syntax someObject.someMessage. The dot must go in between.</p>
63 <p class="p2"><br></p>
64 <p class="p3">Now this next bit is a little bit tricky. In a given object, each <i>message</i> calls (calls means executes) a particular <i>method</i>. Different types of objects may have methods with the same name, and thus respond to the same message in different ways. Whoah, get that? Read it again slowly, as this is pretty important:</p>
65 <p class="p2"><br></p>
66 <p class="p3"><i>Different types of objects may have methods with the same name, and thus respond to the same message in different ways.</i></p>
67 <p class="p2"><br></p>
68 <p class="p3">What's interesting about this is that the actual methods may differ in what they do, but as long as they implement<span class="Apple-converted-space">  </span>a method with that name, they become interchangeable in your code.<span class="Apple-converted-space"> </span></p>
69 <p class="p2"><br></p>
70 <p class="p3">A good example is 'value'. All objects in SC respond to the message 'value'. When you 'call' a method, it always 'returns' something, such as a value or a result. When you call the method 'value' on a Function it will evaluate and return the result of its last line of code. The example below will return the number 5.</p>
71 <p class="p2"><br></p>
72 <p class="p4"><span class="s1"><span class="Apple-tab-span"> </span></span>f = { <span class="s6">"Evaluating..."</span>.postln; 2 + 3; };</p>
73 <p class="p4"><span class="Apple-tab-span"> </span>f.value;</p>
74 <p class="p6"><span class="Apple-tab-span"> </span></p>
75 <p class="p3">Often methods simply return the object itself. This is the case with most objects and the message 'value'. The example below demonstrates this. (Everything to the right of the <span class="s7">//</span> is a 'comment', which means that SC just ignores it. Comments are a good idea to make your code clearer.)</p>
76 <p class="p2"><br></p>
77 <p class="p7"><span class="s3"><span class="Apple-tab-span"> </span></span><span class="s4">f = 3;<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// Here I make f equal to a number</p>
78 <p class="p7"><span class="s4"><span class="Apple-tab-span"> </span>f.value;<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// Post window says: 3, i.e it returns itself</p>
79 <p class="p7"><span class="s4"><span class="Apple-tab-span"> </span>f.value;<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// Still says 3</p>
80 <p class="p8"><span class="Apple-tab-span"> </span></p>
81 <p class="p7"><span class="s4"><span class="Apple-tab-span"> </span>f = { 3.0.rand; };<span class="Apple-tab-span"> </span></span>// Here it's a Function.<span class="Apple-converted-space"> </span></p>
82 <p class="p7"><span class="s4"><span class="Apple-tab-span"> </span>f.value;<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// 3.0.rand means return a random value from 0 to 3.0 exclusive.</p>
83 <p class="p7"><span class="s4"><span class="Apple-tab-span"> </span>f.value;<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// something different</p>
84 <p class="p7"><span class="s4"><span class="Apple-tab-span"> </span>f.value;<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// something different</p>
85 <p class="p6"><span class="Apple-tab-span"> </span></p>
86 <p class="p3">This means that by using the 'value' method Functions and other objects can be interchangeable in your code. This is an example of <i>polymorphism</i>, which is one of the powerful features of what's called Object Oriented Programming. Polymorphism just means that different objects are interchangeable (at least providing they return something sensible for what you're doing) if they respond to the same message. Object Oriented Programming (or OOP, as it's called for short) just means programming with objects. Simple, yes? Here's another short example showing this in action:</p>
87 <p class="p2"><br></p>
88 <p class="p7"><span class="s4"><span class="Apple-tab-span"> </span>f = { </span><span class="s2">arg</span><span class="s4"> a; a.value + 3 }; </span>// call 'value' on the arg; polymorphism awaits!</p>
89 <p class="p7"><span class="s4"><span class="Apple-tab-span"> </span>f.value(3);<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// 3.value = 3, so this returns 3 + 3 = 6</p>
90 <p class="p4"><span class="Apple-tab-span"> </span>g = { 3.0.rand; };</p>
91 <p class="p7"><span class="s4"><span class="Apple-tab-span"> </span>f.value(g);<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// here the arg is a Function. Cool, huh?</p>
92 <p class="p7"><span class="s4"><span class="Apple-tab-span"> </span>f.value(g);<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// try it again, different result</p>
93 <p class="p2"><br></p>
94 <p class="p3">Start to see how this could be useful?</p>
95 <p class="p2"><br></p>
96 <p class="p3">Functions can also have what are called arguments. These are values which are passed into the Function when it is evaluated. The example below demonstrates how this works. See if you can guess what the result will be before executing it.</p>
97 <p class="p2"><br></p>
98 <p class="p4"><span class="s1"><span class="Apple-tab-span"> </span></span>(</p>
99 <p class="p4"><span class="Apple-tab-span"> </span>f = { <span class="s2">arg</span> a, b;<span class="Apple-converted-space"> </span></p>
100 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>a - b;<span class="Apple-converted-space"> </span></p>
101 <p class="p4"><span class="Apple-tab-span"> </span>};</p>
102 <p class="p4"><span class="Apple-tab-span"> </span>f.value(5, 3);</p>
103 <p class="p4"><span class="Apple-tab-span"> </span>)</p>
104 <p class="p2"><br></p>
105 <p class="p3">Arguments are declared at the beginning of the Function, using the keyword '<span class="s8">arg</span>'. You can then refer to them just like variables. When you call value on a Function, you can pass in arguments, in order, by putting them in parentheses: <span class="s5">someFunc.value(arg1, arg2)</span>. This is the same with any method that takes arguments, not just value.</p>
106 <p class="p2"><br></p>
107 <p class="p3">You can specify different orders by using what are called keyword arguments:</p>
108 <p class="p2"><br></p>
109 <p class="p4"><span class="s1"><span class="Apple-tab-span"> </span></span>f = { <span class="s2">arg</span> a, b; a / b; }; <span class="Apple-tab-span"> </span><span class="s9">// '/' means divide</span></p>
110 <p class="p4"><span class="Apple-tab-span"> </span>f.value(10, 2); <span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s9">// regular style</span></p>
111 <p class="p4"><span class="Apple-tab-span"> </span>f.value(b: 2, a: 10); <span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s9">// keyword style</span></p>
112 <p class="p2"><br></p>
113 <p class="p3">You can mix regular and keyword style if you like, but the regular args must come first:</p>
114 <p class="p2"><br></p>
115 <p class="p4"><span class="s1"><span class="Apple-tab-span"> </span></span>f = { <span class="s2">arg</span> a, b, c, d; (a + b) * c - d };</p>
116 <p class="p4"><span class="Apple-tab-span"> </span>f.value(2, c:3, b:4, d: 1); <span class="s9">// 2 + 4 * 3 - 1</span></p>
117 <p class="p2"><br></p>
118 <p class="p3">(Note that SC has no operator precedence, i.e. math operations are done in order, and division and multiplication are not done first. To force an order use parentheses. e.g. 4 + (2* 8) )</p>
119 <p class="p2"><br></p>
120 <p class="p3">Sometimes it's useful to set default values for arguments. You can do this like so:</p>
121 <p class="p2"><br></p>
122 <p class="p4"><span class="s1"><span class="Apple-tab-span"> </span></span>f = { <span class="s2">arg</span> a, b = 2; a + b; };</p>
123 <p class="p4"><span class="Apple-tab-span"> </span>f.value(2); <span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s9">// 2 + 2</span></p>
124 <p class="p6"><span class="Apple-tab-span"> </span></p>
125 <p class="p3">Default values must be what are called literals. Literals are basically numbers, strings, symbols (more on these later), or collections of them. Don't worry if that doesn't totally make sense, it will become clearer as we go on.</p>
126 <p class="p2"><br></p>
127 <p class="p3">There is an alternate way to specify args, which is to enclose them within two vertical lines. (On most keyboards the vertical line symbol is Shift-\ ) The following two Functions are equivalent:</p>
128 <p class="p2"><br></p>
129 <p class="p4"><span class="s1"><span class="Apple-tab-span"> </span></span>f = { <span class="s2">arg</span> a, b; a + b; };</p>
130 <p class="p4"><span class="Apple-tab-span"> </span>g = { <span class="s2">|a, b|</span> a + b; };</p>
131 <p class="p4"><span class="Apple-tab-span"> </span>f.value(2, 2);</p>
132 <p class="p4"><span class="Apple-tab-span"> </span>g.value(2, 2);</p>
133 <p class="p2"><br></p>
134 <p class="p3">Why have two different ways? Well some people like the second one better and consider it a shortcut. SC has a number of syntax shortcuts like this, which can make writing code a little faster. In any case you will encounter both forms, so you need to be aware of them.</p>
135 <p class="p2"><br></p>
136 <p class="p3">You can also have variables in a Function. These you need to declare at the beginning of the Function, just after the args, using the keyword '<span class="s8">var</span>'.</p>
137 <p class="p2"><br></p>
138 <p class="p4"><span class="s1"><span class="Apple-tab-span"> </span></span>(</p>
139 <p class="p4"><span class="Apple-tab-span"> </span>f = { <span class="s2">arg</span> a, b;</p>
140 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s2">var</span> firstResult, finalResult;</p>
141 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>firstResult = a + b;</p>
142 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>finalResult = firstResult * 2;</p>
143 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>finalResult;</p>
144 <p class="p4"><span class="Apple-tab-span"> </span>};</p>
145 <p class="p7"><span class="s4"><span class="Apple-tab-span"> </span>f.value(2, 3); <span class="Apple-tab-span"> </span></span>// this will return (2 + 3) * 2 = 10</p>
146 <p class="p4"><span class="Apple-tab-span"> </span>)</p>
147 <p class="p2"><br></p>
148 <p class="p3">Variable and argument names can consist of letters and numbers, but must begin with a lower-case letter and cannot contain spaces.</p>
149 <p class="p2"><br></p>
150 <p class="p3">Variables are only valid for what is called their scope. The scope of a variable declared in a Function is that Function, i.e. the area between the two curly brackets. Execute these one at a time:</p>
151 <p class="p2"><br></p>
152 <p class="p4"><span class="Apple-tab-span"> </span>f = { <span class="s2">var</span> foo; foo = 3; foo; };</p>
153 <p class="p4"><span class="Apple-tab-span"> </span>f.value;</p>
154 <p class="p7"><span class="s4"><span class="Apple-tab-span"> </span>foo;<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// this will cause an error as 'foo' is only valid within f.</p>
155 <p class="p2"><br></p>
156 <p class="p3">You can also declare variables at the top of any block of code which you execute altogether (i.e. by selecting it all). In such a case that block of code is the variable's scope. Execute the block (in parentheses) and then the last line.</p>
157 <p class="p2"><br></p>
158 <p class="p4"><span class="Apple-tab-span"> </span>(</p>
159 <p class="p4"><span class="Apple-tab-span"> </span><span class="s2">var</span> myFunc;</p>
160 <p class="p4"><span class="Apple-tab-span"> </span>myFunc = { <span class="s2">|input|</span> input.postln; };</p>
161 <p class="p4"><span class="Apple-tab-span"> </span>myFunc.value(<span class="s6">"foo"</span>); <span class="Apple-tab-span"> </span><span class="s9">// arg is a String</span></p>
162 <p class="p4"><span class="Apple-tab-span"> </span>myFunc.value(<span class="s6">"bar"</span>);</p>
163 <p class="p4"><span class="Apple-tab-span"> </span>)</p>
164 <p class="p6"><span class="Apple-tab-span"> </span></p>
165 <p class="p7"><span class="s4"><span class="Apple-tab-span"> </span>myFunc;<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// throws an error</p>
166 <p class="p9"><br></p>
167 <p class="p3">You may be wondering why we haven't needed to declare variables like '<span class="s5">f</span>', and why they don't seem to have any particular scope (i.e. they keep there values even when executing code one line at a time). The letters a to z are what are called interpreter variables. These are pre-declared when you start up SC, and have an unlimited, or 'global', scope. This makes them useful for quick tests or examples. You've already encountered one of these, the variable '<span class="s5">s</span>', which you'll recall by default refers to the localhost server.</p>
168 <p class="p2"><br></p>
169 <p class="p3">For more information see:<b><span class="Apple-converted-space"> </span></b></p>
170 <p class="p2"><br></p>
171 <p class="p10"><a href="../../Language/Functions.html">Functions</a><span class="s4"><b> </b><a href="../../Core/Kernel/Function.html"><span class="s1">Function</span></a><b> </b><a href="../../Language/Assignment.html"><span class="s1">Assignment</span></a><b> </b><a href="../../Language/Intro-to-Objects.html"><span class="s1">Intro-to-Objects</span></a><b> </b><a href="../../Language/Literals.html"><span class="s1">Literals</span></a><b> </b><a href="../../Language/Scope.html"><span class="s1">Scope</span></a></span></p>
172 <p class="p2"><br></p>
173 <p class="p3">____________________</p>
174 <p class="p2"><br></p>
175 <p class="p3">This document is part of the tutorial <b>Getting Started With SuperCollider</b>.</p>
176 <p class="p2"><br></p>
177 <p class="p3">Click here to go on to the next section: <a href="Functions and Sound.html"><span class="s10">Functions and Sound</span></a></p>
178 <p class="p2"><br></p>
179 <p class="p3">Click here to return to the table of Contents: <a href="Getting Started With SC.html"><span class="s10">Getting Started With SC</span></a></p>
180 </body>
181 </html>